import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';
import 'data_source/post.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
        centerTitle: true,
        elevation: 0,
        actions: <Widget>[
          IconSource("Form","09"),
        ],
      ),
      body: _FormDemo(),
    );
  }
}

class _FormDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _FormStateDemo();
  }
}

class _FormStateDemo extends State<_FormDemo> {
  final TextEditingController textEditingController = TextEditingController();
  final submitFormKey=GlobalKey<FormState>();
   String username;
   String password;
   bool _autovalidate=false;
   void _submitForm(){
     submitFormKey.currentState.save();
     if(submitFormKey.currentState.validate()){
       debugPrint("username:$username");
       debugPrint("password:$password");
       Scaffold.of(context).showSnackBar(
           SnackBar(content: Text("username:$username,password:$password"))
       );
     }else{
       setState(() {
         _autovalidate=true;
       });
     }

   }
   String _validateUsername(String value){
     if(value.isEmpty){
       return "username is request";
     }
     return null;
   }
   String _validatePassword(String value){
     if(value.isEmpty){
       return "password is request";
     }
     return null;
   }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      //alignment: Alignment(0, 0),
      padding: EdgeInsets.all(10.0),
      //color: Theme.of(context).accentColor,
      child: Form(
        key: submitFormKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              decoration: InputDecoration(
                labelText: "username",
                hintText: "请输入用户名",
                helperText: "",
                icon: Icon(Icons.person),
              ),
              onSaved: (value) {
               username=value;
              },
              validator:_validateUsername,
              autovalidate: _autovalidate,
            ),
            TextFormField(
              obscureText: true,
              decoration: InputDecoration(
                  labelText: "password",
                  hintText: "请输入密码",
                  helperText: "",
                  icon: Icon(Icons.description)),
                  onSaved: (value) {
                    password=value;
                  },
                  validator: _validatePassword,
               autovalidate: _autovalidate,
            ),
//            TextField(
//              decoration: InputDecoration(
//                labelText: "mail",
//                icon: Icon(Icons.mail),
//              ),
////              onChanged: (value){
////                debugPrint("onChanged:$value");
////              },
//              controller: textEditingController,
//              onSubmitted: (value) {
//                debugPrint("onSubmitted:$value");
//              },
//              onEditingComplete: () {
//                debugPrint("onEditingComplete");
//              },
//            ),
            SizedBox(
              height: 20.0,
            ),
            Container(
              width: double.infinity,
              child: RaisedButton(
                color: Theme.of(context).primaryColor,
                child: Container(
                  padding: EdgeInsets.only(top:12.0,bottom: 12.0),
                  child: Text(
                    "submit",
                    style: TextStyle(color: Colors.white,),
                  ),
                ),
                onPressed: _submitForm,
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void initState() {
    textEditingController.addListener(() {
      debugPrint("textEditingController:${textEditingController.text}");
    });
    super.initState();
  }

  @override
  void dispose() {
    textEditingController.dispose();
    super.dispose();
  }
}
